package com.itheima.tlias08_14.config;

import com.aliyun.oss.OSS;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.UUID;

@Component
public class OssClient {

    @Resource
    AliyunOssConfig config;

    @Resource
    LoadOssConfig loadOssConfig;

    @Autowired
    OSS createOss;


    public String upload(String fileName, InputStream inputStream){
        //获取上传文件的后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //为了防止储存的文件名重复，需要给文件重命名
        String uuid = UUID.randomUUID().toString();
        // 填写Object完整路径，完整路径中不能包含Bucket名称
        String objectName = uuid + suffix;//获取到的文件存储的文件名
        createOss.putObject(config.getBucketName(), objectName, inputStream);//文件数据上传到阿里云Bucket中
        //String url = "https://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/" + objectName;//生成文件在阿里云中的访问地址
        String url = config.getUrl() + objectName;
        return url;

    }
}
